<?php

namespace app\enterprise\controller;

use app\adminapi\controller\AuthController;
use think\exception\ValidateException;

use app\enterprise\model\Project as projectModel;
use app\enterprise\model\Parameter as parameterModel;
use app\model\project\ProjectType as ProjectTypeModel;

/**
 * 项目相关
 */
class ProjectType extends AuthController
{
    public function index()
    {
        $param = $this->request->param();

        $where[] = ['company_id', '=', $this->adminId];
        $where[] = ['status', '=', 1];


        $size = $param['size'] ?? '';


        if (!empty($size)) {
            $list = ProjectTypeModel::where('status', '=', 1)
                ->where($where)
                ->order('id desc')
                ->paginate(10);;
            $data = $list->items();
        } else {
            $list = ProjectTypeModel::where($where)->select();
            $data = $list;
        }

        return app('json')->success([
            'list' => $data,
            'total' => empty($size) ? 0 : $list->total(),
            'total_page'=>empty($size) ? 0 : $list->lastPage(),
        ]);

    }


    public function save()
    {

        $typeName = $this->request->post('type_name');
        $id = $this->request->post('id' , 0);
        $param['company_id'] = $this->adminId;
        $param['type_name'] = $typeName;
        try {
            validate([
                'type_name|分类名称' => 'require|max:255',
            ])->check($param);
            $model = new ProjectTypeModel();


            if ($id == 0) {
                $param['creator_id'] = $this->adminId;
                $model->save($param);
            } else {

                $model->where('id' , $id)->where('company_id' , $this->adminInfo['company_id'])->update($param);
            }
            return app('json')->success('操作成功');
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }

    }
    public function delete()
    {
        $id = $this->request->post('id' , 0);
        $model = new ProjectTypeModel();
        $model->where('id' , $id)->where('company_id' , $this->adminInfo['company_id'])->update([
            'status' => -1 ,
            'delete_time' => time() ,
        ]);
        return app('json')->success('删除成功');

    }


}